package com.gec.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gec.model.system.SysRole;
import com.gec.model.vo.SysRoleQueryVo;
import com.gec.service.SysRoleService;
import com.gec.system.common.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@Api(tags = "角色管理控制器")
@RestController
@RequestMapping("/admin/system/sysRole")
public class SysRoleController {
    @Autowired
    private SysRoleService sysRoleService;

//    @ApiOperation("查询全部角色接口")
//    @GetMapping("/findAll")
//    public List<SysRole> findAll(){
//        List<SysRole> list=this.sysRoleService.list();
//        return list;
//    }
//
//    @ApiOperation("逻辑删除接口")
//    @DeleteMapping("/remove/{id}")
//    public boolean removeRole(@PathVariable Long id){
//        boolean removeById = this.sysRoleService.removeById(id);
//        return  removeById;
//    }

    @ApiOperation("查询全部角色接口")
    @GetMapping("/findAll")
    public Result findAll(){
        List<SysRole> list=this.sysRoleService.list();
        return Result.ok(list);
    }

    @PreAuthorize("hasAnyAuthority('bnt.sysRole.remove')")
    @ApiOperation("逻辑删除接口")
    @DeleteMapping("/remove/{id}")
    public Result removeRole(@PathVariable Long id){
        boolean removeById = this.sysRoleService.removeById(id);
        if(removeById){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }
    @PreAuthorize("hasAnyAuthority('bnt.sysRole.list')")

    @ApiOperation("角色分页查询")
    @GetMapping("/{page}/{limit}")
    public Result findRolePageQuery(@PathVariable Long page, @PathVariable Long limit,
                                    SysRoleQueryVo sysRoleQueryVo){
        IPage<SysRole> page1=new Page<>(page,limit);
        page1=this.sysRoleService.selectPage(page1,sysRoleQueryVo);
        return Result.ok(page1);
    }
    @PreAuthorize("hasAnyAuthority('bnt.sysRole.add')")
    @ApiOperation("添加角色")
    @PostMapping("/addRole")
    public Result addRole(@RequestBody SysRole sysRole){
        boolean save = this.sysRoleService.save(sysRole);
       if(save){
           return Result.ok();
       }else {
           return Result.fail();
       }
    }
    @PreAuthorize("hasAnyAuthority('bnt.sysRole.list')")
    @ApiOperation("根据id查询单个用户")
    @GetMapping("findSysRoleByRoleName/{id}")
    public Result findSysRoleById(@PathVariable Long id){
        SysRole byId = this.sysRoleService.getById(id);
        return Result.ok(byId);
    }
    @PreAuthorize("hasAnyAuthority('bnt.sysRole.update')")
    @ApiOperation("修改角色")
    @PostMapping("/updateRole")
    public Result updateRole(@RequestBody SysRole sysRole){
        boolean role = this.sysRoleService.updateById(sysRole);
        if(role){
            return Result.ok();
        }else{
            return Result.fail();
        }
    }
    @PreAuthorize("hasAnyAuthority('bnt.sysRole.remove')")

    @ApiOperation("删除角色")
    @DeleteMapping("/deleteRole")
    public Result deleteRole(@RequestBody List<Long> ids){
        boolean removeById = this.sysRoleService.removeByIds(ids);
       if (removeById){
           return Result.ok();
       }else{
           return Result.fail();
       }
    }


}
